package com.cwits.wifi.ui.dialog;

import android.app.DialogFragment;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.cwits.wifi.R;
import com.cwits.wifi.base.BaseDialogFragment;

import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.VideoView;

/**
 * Created by Administrator on 2016-05-03.
 */
public class VideoPlayDialog extends BaseDialogFragment implements View.OnClickListener {
    //VDO_SUCESS_PATH


    private final static int PROGRESS_CHANGED = 0;
    private final static int HIDE_VIDEOCONTROL = 1;

    private TextView mTvCancle;
    private VideoView mVideoView;
    private SeekBar mSeekBar;
    private Uri uri;
    private ImageView mImPlay;

    private static String VideoPath;

    public static VideoPlayDialog newInstance(String path) {
        VideoPath = path;
        return new VideoPlayDialog();
    }

    public void setVideoPath(String path) {
        uri = Uri.parse(path);
        initVideoView();
        startPlay();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.video_play_dialog, container, false);
        uri = Uri.parse(VideoPath);
        mImPlay = (ImageView) v.findViewById(R.id.index_im_play);
        mImPlay.setOnClickListener(this);
        mTvCancle = (TextView) v.findViewById(R.id.select_updata_tv_cancle);
        mTvCancle.setOnClickListener(this);
        mVideoView = (VideoView) v.findViewById(R.id.video_dialog_view);
        mSeekBar = (SeekBar) v.findViewById(R.id.video_dialog_sb);
        initVideoView();
        startPlay();
        return v;
    }

    private void startPlay() {
        if (uri != null && mVideoView != null) {
            mVideoView.setVideoURI(uri);
            mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_LOW);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.select_updata_tv_cancle:
                dismiss();
                break;
            case R.id.index_im_play:
                mVideoView.start();
                mImPlay.setVisibility(View.GONE);
                mHandler.removeMessages(PROGRESS_CHANGED);
                mHandler.sendEmptyMessage(PROGRESS_CHANGED);
                break;
        }
    }

    private void initVideoView() {

        mVideoView.setOnSeekCompleteListener(new io.vov.vitamio.MediaPlayer.OnSeekCompleteListener() {

            @Override
            public void onSeekComplete(io.vov.vitamio.MediaPlayer mp) {

            }

        });

        mVideoView.setOnErrorListener(new io.vov.vitamio.MediaPlayer.OnErrorListener() {

            @Override
            public boolean onError(io.vov.vitamio.MediaPlayer mp, int what, int extra) {
                Toast.makeText(getActivity(), getString(R.string.dialog_jm_err), Toast.LENGTH_LONG).show();
                dismiss();
                return false;
            }
        });

        mVideoView.setOnBufferingUpdateListener(new io.vov.vitamio.MediaPlayer.OnBufferingUpdateListener() {

            @Override
            public void onBufferingUpdate(io.vov.vitamio.MediaPlayer mp, int percent) {
            }

        });

        mVideoView.setOnInfoListener(new io.vov.vitamio.MediaPlayer.OnInfoListener() {

            @Override
            public boolean onInfo(io.vov.vitamio.MediaPlayer mp, int what, int extra) {

                switch (what) {

                    case MediaPlayer.MEDIA_INFO_BUFFERING_START:

                        break;

                    case MediaPlayer.MEDIA_INFO_BUFFERING_END:

                        break;

                    case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:

                        break;


                    case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE:

                        break;

                    case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:

                        break;

                }

                return true;
            }

        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekbar,
                                          int progress, boolean fromUser) {
                if (fromUser) {
                    mVideoView.seekTo(progress);

                }
            }

            public void onStartTrackingTouch(SeekBar arg0) {

            }

            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        mVideoView.setOnPreparedListener(new io.vov.vitamio.MediaPlayer.OnPreparedListener() {

            @Override
            protected Object clone() throws CloneNotSupportedException {
                return super.clone();
            }

            @Override
            public void onPrepared(io.vov.vitamio.MediaPlayer mp) {

                //开始播放
                int i = (int) mVideoView.getDuration();
                mSeekBar.setMax(i);
                mVideoView.start();
                mHandler.removeMessages(PROGRESS_CHANGED);
                mHandler.sendEmptyMessage(PROGRESS_CHANGED);
                //mHandler.removeMessages(BUFFER);
                //mHandler.sendEmptyMessage(BUFFER);


            }

        });

        mVideoView.setOnCompletionListener(new io.vov.vitamio.MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(io.vov.vitamio.MediaPlayer mp) {
                //播放完毕
                mSeekBar.setProgress((int) mVideoView.getDuration());
                mHandler.removeMessages(PROGRESS_CHANGED);
                mVideoView.seekTo(0);
                mImPlay.setVisibility(View.VISIBLE);
                //mHandler.removeMessages(HIDE_VIDEOCONTROL);
            }

        });

        mVideoView.setDrawingCacheEnabled(true);
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case PROGRESS_CHANGED:

                    if (mVideoView == null) {
                        return;
                    }
                    mSeekBar.setProgress((int) mVideoView.getCurrentPosition());
                    mHandler.sendEmptyMessageDelayed(PROGRESS_CHANGED, 500);
                    break;
            }
            super.handleMessage(msg);
        }
    };
}
